<?php
namespace app\admin\controller\soft;
use app\admin\Adminbase;
use app\common\model\UserVip as UserVipModel;

class SystemUser extends Adminbase
{
	
	/**
    * 修改密码
    * /admin/soft/SystemUser/updatePwd
    * @access public
    * @param string $password   	原密码
    * @param string $newPwd 		新密码
    * @return  json
    */
	public function updatePwd($password,$newPwd){
		if( empty($password) )
			throw new \think\Exception('原密码不能为空');
		if( empty($newPwd) )
			throw new \think\Exception('新密码不能为空');
		
		$userInfo = request()->userInfo;
		
		if( mb_strlen($userInfo['user_code'])==11 ){
			$res = UserVipModel::where('uid',$userInfo['uid'])->find();
			if($res->password != md5( $password ) ){
				return $this -> ReError("原密码不正确！");
			}
			$res->password = md5( $newPwd );
			$res->save();
			return $this -> ReSucess('', "密码更新成功");
		}else{
			$password = md5( 'wei'.md5( $password ) );
			$newPwd = md5( 'wei'.md5( $newPwd ) );
	
			if( $userInfo['user_pwd'] != $password )
				return $this -> ReError("原密码不正确！");
	
			$result = model('SystemUser')->updatePwd( $userInfo['user_id'] , $newPwd);
			if( $result ) {
				action('admin/soft/Auth/logout'); 
				return $this -> ReSucess($result, "密码更新成功,请退出重新登录");
			}
		}
		
		return $this -> ReError("密码更新失败，请重试！");
	}
	
	
	/**
    * 修改指定用户密码
    * /admin/soft/SystemUser/updatePwd
    * @access public
    * @param string $password   	原密码
    * @param string $newPwd 		新密码
    * @return  json
    */
	public function updateUserPwd($user_id,$newPwd){
		if( empty($newPwd) )
			throw new \think\Exception('新密码不能为空');
		
		$newPwd = md5( 'wei'.md5( $newPwd ) );

		$result = model('SystemUser')->updatePwd( $user_id , $newPwd);
		
		if( $result ) {
			return $this -> ReSucess($result, "密码更新成功,请退出重新登录");
		}
		
		return $this -> ReError("密码更新失败，请重试！");
	}
	
	/**
    * 添加用户
    * /admin/soft/SystemUser/addUser
    * @access public
    * @param string post()   	post数组
    * @return  json
    */
	public function addUser(){
		$data = request()->post();
		
		//验证
        $result = $this->validate($data,'SystemUser');
        if ($result !== true) {
           return $this ->  ReError($result);
        }
		
		//保存
		$result = model('SystemUser') -> addUser( $data );
		if($result == 1) {
			return $this -> ReSucess($result,"新增成功");
		}else{
			return $this ->  ReError($result);
		}
	}
	
	/**
    * 启用或禁用
    * /admin/soft/SystemUser/addOrgData
    * @access public
    * @param array 	$data  [{user_id:1,is_enable:1,land_status:0}]
    * @return  json
    */
	public function setEnable($data){
		if( empty($data) )
			throw new \think\Exception('参数不能为空');
		
		$result =  model("SystemUser")->updateData( $data );
		if(!$result){
			return $this -> ReError("操作失败！");
		}else{
			return $this -> ReSucess($result,"操作成功！");
		}
		
	}
	
	/**
    * 获取组织数据
    * /admin/soft/SystemUser/getOrgData
    * @access public
    * @param null 
    * @return  json
    */
	public function getOrgData(){
		$data= array();
		$result =  \Db::table('xw_system_group')->where('org_name','<>','店长管理')->select();
		if($result[0]){
			foreach ($result as $key => $value) {
				array_push($data,$value);
			}
		}
		return $this -> ReSucess($data,"操作成功！");
	}
	
	/**
    * 新增或更新组织
    * /admin/soft/SystemUser/addOrgData
    * @access public
    * @param string 	$org_id   存在则更新，不然就新增
    * @param string 	$parent_org_id 
    * @param string 	$org_name 
    * @return  json
    */
	public function addOrgData($org_id='',$parent_org_id,$org_name){
		
		$isExist = model("SystemGroup")->isExistNode($org_name,$parent_org_id,$org_id);
		if($isExist){
			return $this -> ReError("已存在名为 $org_name 的节点！");
		}	
		
		$result =  model("SystemGroup")->addOrUptateData($org_id,request()->post());
		if(!$result){
			return $this -> ReError("操作失败！");
		}else{
			return $this -> ReSucess($result,"操作成功！");
		}
		
	}
	
	/**
    * 删除组织
    * /admin/soft/SystemUser/deleteOrgData
    * @access public
    * @param string 	$orgIds  
    * @return  json
    */
	public function deleteOrgData($orgIds){
		if( empty($orgIds)  )
			throw new \think\Exception('orgIds参数不能为空');
		
		$result = model("SystemUser")->selectOrgUser($orgIds);
		if($result[0]){
			return $this -> ReError("该节点或子节点下存在用户数据，不允许删除！");
		}	
		
		$result = model('SystemGroup')->deleteNode($orgIds);
		if( !$result ){
			return $this -> ReError("删除失败！");
		}else{
			return $this -> ReSucess($result,"删除成功！");
		}
		
	}
	
	/**
    * 授权
    * /admin/soft/SystemUser/action
    * @access public
    * @param string 	$org_id  
    * @param string 	$actions  
    * @return  json
    */
	public function action($org_id,$actions){
		if( empty($org_id)  )
			throw new \think\Exception('org_id参数不能为空');
		
		if( !model("SystemGroup")->addAction($org_id,$actions) ){
			return $this -> ReError("授权失败！");
		}else{
			return $this -> ReSucess($data,"");
		}
	}
	
	
	/**
    * 获取授权列表
    * /admin/soft/SystemUser/getActionList
    * @access public
    * @param string 	$orgIds  
    * @return  json
    */
	public function getActionList($org_id){
		if( empty($org_id)  )
			throw new \think\Exception('org_id参数不能为空');
		
		$orgs =  model("SystemGroup")->where("org_id=%s",$org_id)->select();
		$orgActions = json_decode($orgs[0]["action"],true);
		
		$navs = model("nav")
			->field('func_code,nav_id,nav_url,nav_name,nav_pid')
			->order('sort asc' )
			->select();
			
		$actions = M("SystemNavAction")->select();
		
		$arr=array();
		$whiteList= ["pageMng/index","setParam/index",'userMng/index',"logMng/index"];
		foreach($navs as $value){
			if($value["nav_pid"]=="0" && !in_array($value["nav_url"],$whiteList)){
				
				//当前授权
				$value['action']=[];
				foreach($actions as $act){
					if($act["nav_id"]==$value['nav_id']){
						if( in_array( $act['action_code'], explode(",",$orgActions[$value['func_code']]))  ){
							$act['check']=true;
						}else{
							$act['check']=false;
						}
						array_push($value['action'],$act);
					}
				}
				
				//字节点
				$value['children']=[];
				foreach($navs as $key => $children){
					if($children["nav_pid"]==$value['nav_id']){
						
						//授权
						$children['action']=array();
						foreach($actions as $acts){
							if($acts["nav_id"]==$children['nav_id']){
								if( in_array($acts['action_code'], explode(",",$orgActions[$children['func_code']])) ){
									$acts['check']=true;
								}else{
									$acts['check']=false;
								}
								array_push($children['action'],$acts);
							}
						}
						
						array_push($value['children'],$children);
					}
				}
				
				array_push($arr,$value);
			}
		}
		
		return $this->ReSucess($arr,"");
	}
	
	/**
    * 获取个人头像
    * /admin/soft/SystemUser/getImg
    * @access public
    * @param string 	$orgIds  
    * @return  json
    */
	public function getImg($user_guid){
		$img =  model("SystemUpload")
			->where('file_buss_type','用户信息')
			->where('file_type','头像')
			->where('file_guid',$user_guid)
			->value("file_path");
		if($img){
			return APP_PATH.'/'.$img;	
		}else{
			return APP_PATH.'/images/user_img.jpg';	
		}
	}
}
